package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.exception.BssException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * 是否默认资费计划
 */
public class IsDefaultRatePlanAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        if(!KiteStringUtils.isEmpty(value)){
            List<KiteProdInstFunc> kiteProdInstList = getBaseNewFuncProd(data);
            if(KiteListUtils.isNotEmpty(kiteProdInstList)){
                kiteProdInstList.stream().forEach(kiteProdInst -> {
                    KiteAttr kiteAttr = kiteProdInst.getAttr("200004025");
                    if(!ObjectUtils.isEmpty(kiteAttr)){
                        if(!KiteStringUtils.equals(inst.getInstId(),kiteProdInst.getProdInstId())){
                            if(KiteStringUtils.equals("1_0000106",kiteAttr.getValue()) && KiteStringUtils.equals("1_0000106",value)){
                                BssException.throwOut("默认资费计划校验","默认资费计划校验","默认资费计划只允许选择一个，请检查！");
                            }
                        }
                    }
                });
            }
        }
    }

    private List<KiteProdInstFunc> getBaseNewFuncProd(AttrData data) {
        List<KiteProdInstFunc> zfKiteProdInstFuncs = Lists.newArrayList(); // 取所有接入类产品
        List<KiteProdInst> kiteProdInstList = querySceneInstService.listProdInsts(data.getSceneInstId());
        final KiteProdInst[] prodInsts = new KiteProdInst[1];
        List<KiteProdInst> prodInstList = Lists.newArrayList();
        String zfStr = DcPublicCache.getCodea("202012170001","zf_base_inst");
        if (KiteListUtils.isNotEmpty(kiteProdInstList)) {
            kiteProdInstList.stream().forEach(prodInst -> {
                List<KiteProdInstFunc> kiteProdInstFuncs = querySceneInstService
                    .listProdInstFuncs(data.getCustId(), prodInst.getProdInstId());
                if (KiteListUtils.isNotEmpty(kiteProdInstFuncs)) {
                    kiteProdInstFuncs.stream().forEach(kiteProdInstFunc -> {
                        if (KiteStringUtils.indexOf(zfStr, kiteProdInstFunc.getProdId())>-1) {
                            prodInstList.add(prodInst);
                        }
                    });
                }
            });
        }
        if (KiteListUtils.isNotEmpty(prodInstList)) {
            String[] str = zfStr.split("/");
            for (KiteProdInst kiteProdInst : prodInstList) {
                List<KiteProdInstFunc> kiteProdInstFuncList = new ArrayList<>();
                for(String prodId : str){
                    kiteProdInstFuncList = querySceneInstService
                        .listProdInstFuncsByProdId(data.getCustId(), kiteProdInst.getProdInstId(), prodId);
                    if(KiteListUtils.isNotEmpty(kiteProdInstFuncList)){
                        zfKiteProdInstFuncs.addAll(kiteProdInstFuncList);
                    }
                }
            }
        }
        zfKiteProdInstFuncs = zfKiteProdInstFuncs.stream().filter(distinctByKey(b -> b.getProdInstId())).collect(Collectors.toList());
        return zfKiteProdInstFuncs;
    }

    public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        ConcurrentHashMap<Object, Boolean> map = new ConcurrentHashMap<>();
        return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }
}
